---
title: "R20.Mismatch_Robust"
---

# Load Packages
```{r}
library(tidyverse)
library(estimatr)
```

# Load Data
```{r}
load("Data/data_final.Rdata")
```

# Plot Relationship between Mismatch Index and Communist Vote Share in 1975 and Distance to Nearest PCP Office (App. Figures 8ai)
```{r}

data_final %>% 
  ggplot(aes(x=resid, y=Communist_1975)) + 
  geom_point(alpha = 0.25) +
  geom_smooth(col = "black") + 
  geom_smooth() + 
  ylab("Communist Vote Share, 1975") + xlab("Mismatch Index") +
  theme_light() 

ggsave(plot = last_plot(),
             "Figures/Afg8a1.1_TrResidPCP75.pdf")

data_final %>% 
  ggplot(aes(x=resid, y=MinDistPCP_Map/1000)) + 
  geom_point(alpha = 0.25) +
  geom_smooth(col = "black") + 
  geom_smooth() +
  ylab("Distance to Nearest PCP Worker Office") + xlab("Mismatch Index") +
  theme_light() 

ggsave(plot = last_plot(),
             "Figures/Afg8a1.2_TrResidPCPOffice.pdf")

```

# Model Relationship between Mismatch Index and Communist Vote Share in 1975 and Distance to Nearest PCP Office (App. Table 8ai)
```{r}
models<-list()

models[[1]]<-lm_robust(resid ~ Communist_1975 + 
                         ln_Confs_All + as.factor(DistN) +ln_PopDens_70 +Cities_10km +PrRecH_Adm3 +Isolados1950 +PrWheatAr_1972 +LandIneqRat +TerrainSlopeIndex +MedAltitude, 
                       data_final) %>% summary()
models[[2]]<-lm_robust(resid ~ MinDistPCP_Map + 
                         ln_Confs_All + as.factor(DistN) +ln_PopDens_70 +Cities_10km +PrRecH_Adm3 +Isolados1950 +PrWheatAr_1972 +LandIneqRat +TerrainSlopeIndex +MedAltitude, 
                       data_final) %>% summary()

output_covs <- data.frame(seq = 1:1, 
                          variable = c("Communist Vote Share, 1975", "Distance to Nearest PCP Office"), 
                          outcome = c("Mismatch", "Mismatch"),
                          coef = NA, se = NA, n = NA)

for (i in 1:length(models)){
  output_covs[i, "coef"]<-models[[i]]$coefficients[2, 1]
  output_covs[i, "se"]<-models[[i]]$coefficients[2, 2]
  output_covs[i, "n"]<-models[[i]]$df.residual
}

ggplot(output_covs, aes(x = variable)) +
  geom_hline(aes(yintercept = 0), col = "red") +
  geom_point(aes(y = coef), position = position_dodge(width = 1)) + 
  geom_linerange(aes(ymin=coef - (qt(0.975, n)*se), ymax=coef + (qt(0.975, n)*se)), position = position_dodge(width = 1), size = 0.5) +
  geom_linerange(aes(ymin=coef - (qt(0.95, n)*se), ymax=coef + (qt(0.95, n)*se)), position = position_dodge(width = 1), size = 0.75) +
  coord_flip() +
  ylab("Effect of IVs on Mismatch Index") + xlab("Independent Variables") +
  theme_light() 

ggsave(plot = last_plot(), 
       "Figures/Afg8a1.3_BalMismatch.pdf", 
       height = 2, width = 7)

```
